package role

import (
	"context"
	"goAdminV1/internal/model"
	"goAdminV1/utility"

	"github.com/gogf/gf/v2/errors/gerror"

	"goAdminV1/api/role/v1"
)

func (c *ControllerV1) RoleDelete(ctx context.Context, req *v1.RoleDeleteReq) (res *v1.RoleDeleteRes, err error) {
	// 查询角色是否存在
	role, err := model.GetRoleById(ctx, req.Id)
	if err != nil {
		return nil, utility.Error(utility.CodeOperation, err)
	}
	if role == nil {
		return nil, utility.Error(utility.CodeOperation, gerror.New("角色不存在"))
	}

	// 删除角色相关的权限关联
	if err := model.DeleteRolePermissions(ctx, req.Id); err != nil {
		return nil, utility.Error(utility.CodeOperation, err)
	}

	// 删除角色
	if err := model.DeleteRoleById(ctx, req.Id); err != nil {
		return nil, utility.Error(utility.CodeOperation, err)
	}

	// 返回成功响应
	return &v1.RoleDeleteRes{
		RoleId:  req.Id,
		Message: "角色删除成功",
	}, nil
}
